package com.ygqh.baby.service.wechat.model.good;

import com.ygqh.baby.ao.PayType;
import com.ygqh.baby.model.YgOrderInfoModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 好物圈订单信息组装工具
 *
 * @Author: sunshuo
 * @Date: 2019/5/22 10:11
 * @Version: 1.0
 */
public class GoodOrderBuilder {

	private YgOrderInfoModel ygOrder;

	private GoodOrder goodOrder;

	private GoodOrderBuilder() {
	}

	public static GoodOrderBuilder builder(YgOrderInfoModel ygOrder) {
		GoodOrderBuilder builder = new GoodOrderBuilder();
		builder.ygOrder = ygOrder;
		builder.goodOrder = new GoodOrder();
		return builder;
	}

	public GoodOrderBuilder setTransId(String transId) {
		goodOrder.setTransId(transId);
		return this;
	}

	public GoodOrderBuilder setDesc(String desc) {
		goodOrder.setDesc(desc);
		return this;
	}

	public GoodOrderBuilder build() {
		buildMainOrderInfo();
		buildExtOrderInfo();
		return this;
	}

	public GoodOrder get() {
		return goodOrder;
	}

	/**
	 * 组装订单扩展信息
	 */
	private void buildExtOrderInfo() {
		GoodOrderExt extInfo = new GoodOrderExt();
		extInfo.setProductInfo(buildProductInfo());
		extInfo.setExpressInfo(buildExpressInfo());
		if (ygOrder.getTotalPrice().compareTo(ygOrder.getProductPrice()) < 0) {
			extInfo.setPromotionInfo(buildPromotionInfo());
		}
		extInfo.setBrandInfo(buildBrandInfo());
		int paymentMethod = GoodOrderExt.OTHER_PAYMENT_METHOD;
		if (PayType.WeChat.equals(ygOrder.getPayType()) || PayType.Small.equals(ygOrder.getPayType())) {
			paymentMethod = GoodOrderExt.WECHAT_PAYMENT_METHOD;
		}
		extInfo.setPaymentMethod(paymentMethod);
		extInfo.setUserOpenId(ygOrder.getOpenId());
		GoodOrderDetailPage orderDetailPage = new GoodOrderDetailPage();
		orderDetailPage.setPath("pages/user/orderdetail/orderdetail?sourceCode=haowuquan&orderId="
				+ ygOrder.getOrderId());
		extInfo.setOrderDetailPage(orderDetailPage);
		goodOrder.setExtInfo(extInfo);
	}

	private GoodPromotion buildPromotionInfo() {
		GoodPromotion promotionInfo = new GoodPromotion();
		BigDecimal discount = ygOrder.getProductPrice().subtract(ygOrder.getTotalPrice());
		promotionInfo.setDiscountFee(discount.multiply(BigDecimal.valueOf(100)).longValue());
		return promotionInfo;
	}

	private GoodOrderBrand buildBrandInfo() {
		GoodOrderBrand brandInfo = new GoodOrderBrand();
		GoodContactDetailPage contactDetailPage = new GoodContactDetailPage();
		contactDetailPage.setKfType(GoodContactDetailPage.NO_ONLINE_SERVICE);
		brandInfo.setContactDetailPage(contactDetailPage);
		return brandInfo;
	}

	private GoodExpress buildExpressInfo() {
		GoodExpress expressInfo = new GoodExpress();
		expressInfo.setPrice(ygOrder.getExpressPrice().multiply(BigDecimal.valueOf(100)).longValue());
		return expressInfo;
	}

	private GoodOrderProduct buildProductInfo() {
		GoodOrderProduct productInfo = new GoodOrderProduct();
		List<GoodOrderItem> itemList = new ArrayList<>();
		ygOrder.getDetailModels().forEach(detail -> {
			GoodOrderItem item = new GoodOrderItem();
			item.setItemCode(detail.getProductId().toString());
			item.setSkuId(detail.getSkuId().toString());
			item.setAmount(detail.getQuantity());
			item.setUnitPrice(detail.getSalePrice().multiply(BigDecimal.valueOf(100)).longValue());
			item.setTotalFee(item.getUnitPrice() * detail.getQuantity());
			item.setOriginalPrice(detail.getSourceSalePrice().multiply(BigDecimal.valueOf(100)).longValue());
			item.setThumbUrl(getImageUrl(detail.getImageUrl()));
			item.setTitle(detail.getProductName());
			item.setCategoryList(detail.getCategoryNameList());
			GoodItemDetailPage itemDetailPage = new GoodItemDetailPage();
			itemDetailPage.setPath("pages/goods/detail/detail?sourceCode=haowuquan&id=" + detail.getProductId());
			item.setItemDetailPage(itemDetailPage);
			item.setCanBeSearch(true);
			itemList.add(item);
		});
		productInfo.setItemList(itemList);
		return productInfo;
	}

	private String getImageUrl(String imageUrl) {
		int index = imageUrl.lastIndexOf(".");
		return imageUrl.substring(0, index) + "-800*800" + imageUrl.substring(index);
	}

	/**
	 * 组装订单主信息
	 */
	private void buildMainOrderInfo() {
		goodOrder.setOrderId(ygOrder.getOrderId().toString());
		goodOrder.setCreateTime(ygOrder.getCreateTime().getTime() / 1000);
		goodOrder.setPayFinishTime(ygOrder.getPaidTime().getTime() / 1000);
		goodOrder.setFee(ygOrder.getTotalPrice().multiply(BigDecimal.valueOf(100)).longValue());
		goodOrder.setStatus(GoodOrder.ORDER_PAY);
	}
}
